# koroFileHeader at Yungoal acer
# Create: 2021-07-15 13:22:47
# LastEdit: 2021-07-16 10:59:45
"""Windows Update 需要关闭
自动更新会导致服务器自动重启，出过几次事故
关闭“Windows Update”服务。服务的初始状态是Running，需要的服务状态是Stopped
至于补丁更新，应该是定期手动执行，不是这里要关注的
"""
__author__ = '749B'


import os, sys
BASE_DIR = os.getcwd()
sys.path.append(BASE_DIR)

# pylint: disable = import-error
from azure_client.models import RuncmdCheck, RuncmdTask
from utils.blueking import job_start, job_success, job_fail

from typing import Optional, List, Tuple
_Check = Tuple[bool, str]


class WindowsUpdateCheck(RuncmdCheck):

    def parse_linux(self, task: RuncmdTask) -> _Check:
        raise Exception("Windows Update 不存在对 Linux 的检查")

    def parse_windows(self, task: RuncmdTask) -> _Check:
        status = self.std_out.strip().splitlines()[0]
        if status == 'Running':
            return False, "自动更新未调整:%s" % status
        if status != 'Stopped':
            return False, "自动更新未关闭:%s" % status
        return True, "OK"


def main(args: Optional[List[str]] = None) -> None:
    """主函数"""
    check_obj = WindowsUpdateCheck(args, None, SCRIPTS_WINSOWS, CHECK_ITEMS)
    check_obj.run()
    
    if check_obj.fail_count:
        job_fail("不合规的记录数: %s" % check_obj.fail_count)
    job_success()


SCRIPTS_WINSOWS = """\
function check($service) {
    $result = Get-Service $service -ErrorAction SilentlyContinue
    if ($?) {
        $result | %{$_.Status}
    } else {
        Write-Host NotExist
    }
}
check("Windows Update")
"""

CHECK_ITEMS = ["Windows Update"]

if __name__ == '__main__':

    job_start()

    ###### 可在此处开始编写您的脚本逻辑代码
    ###### iJobs中执行脚本成功和失败的标准只取决于脚本最后一条执行语句的返回值
    ###### 如果返回值为0，则认为此脚本执行成功，如果非0，则认为脚本执行失败

    main()





